package ltd.dujiabao.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import ltd.dujiabao.commonutils.Result;
import ltd.dujiabao.eduservice.entity.EduTeacher;
import ltd.dujiabao.eduservice.entity.vo.TeacherQuery;
import ltd.dujiabao.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author dujiabao
 * @since 2021-04-18
 */
@Api(value = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
  @Autowired
  EduTeacherService eduTeacherService;

  @ApiOperation("所有讲师列表")
  @GetMapping("/")
  public Result getAllTeachers() {
    List<EduTeacher> teachers = eduTeacherService.list(null);
    return Result.success().addData("data", teachers);
  }

  @ApiOperation("添加一个讲师")
  @PostMapping("/")
  public Result insertTeacher(@ApiParam(name = "teacher", value = "要被加的讲师的json内容", required = true)
                              @RequestBody EduTeacher teacher) {
    boolean isSuccess = eduTeacherService.save(teacher);
    if (isSuccess) {
      return Result.success();
    } else {
      return Result.failure();
    }
  }

  @ApiOperation("通过ID修改讲师")
  @PutMapping("/{id}")
  public Result updateTeacherById(@ApiParam(name = "id", value = "要修改的讲师的id", required = true) @PathVariable String id,
                                  @RequestBody(required = true) EduTeacher teacher) {
    teacher.setId(id);
    boolean isSuccess = eduTeacherService.updateById(teacher);
    if (isSuccess) {
      return Result.success();
    } else {
      return Result.failure();
    }
  }

  @ApiOperation("通过ID查询讲师")
  @GetMapping("/{id}")
  public Result getTeacherById(@ApiParam(name = "id", value = "要查询的讲师的id", required = true) @PathVariable String id) {
    EduTeacher teacher = eduTeacherService.getById(id);
    return Result.success().addData("data", teacher);
  }


  @ApiOperation("通过ID删除讲师")
  @DeleteMapping("/{id}")
  public Result removeTeacherById(@ApiParam(name = "id", value = "要被删除的讲师的id", required = true) @PathVariable String id) {
    boolean isSuccess = eduTeacherService.removeById(id);
    if (isSuccess) {
      return Result.success();
    } else {
      return Result.failure();
    }
  }

  @ApiOperation("分页获取讲师列表")
  @GetMapping("/{current}/{size}")
  public Result getPageTeachers(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                                @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable Long size) {
    Page<EduTeacher> page = new Page<>(current, size);
    eduTeacherService.page(page, null);
    List<EduTeacher> teachers = page.getRecords();
    long pages = page.getPages();
    long total = page.getTotal();
    return Result.success().addData("data", teachers).addData("pages", pages).addData("total", total)
            .addData("current", current).addData("size", size);
  }

  @ApiOperation("根据条件筛选获取讲师列表")
  @PostMapping("/teachersCondition/{current}/{size}")
  public Result getTeachersCondition(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                                     @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable Long size,
                                     @ApiParam(name = "teacherQuery", value = "查询条件", required = false)
                                     @RequestBody(required = false) TeacherQuery teacherQuery) {
    Page<EduTeacher> page = new Page<>(current, size);
    eduTeacherService.pageQuery(page, teacherQuery);
    List<EduTeacher> teachers = page.getRecords();
    long pages = page.getPages();
    long total = page.getTotal();
    return Result.success().addData("data", teachers).addData("pages", pages).addData("total", total)
            .addData("current", current).addData("size", size);
  }
}

